import java.util.ArrayList;

class Solution {
    static class TreeNode {
      int val;
      TreeNode left;
     TreeNode right;
      TreeNode(int x)
      { val = x; }
  }
     ArrayList<Integer> arr=new ArrayList<>();
    public int kthLargest(TreeNode root, int k) {
        if(root==null){
            return 0;
        }
        dfs(root,arr);
        int len=arr.size();
        while(k>0){
            arr.remove(arr.size()-1);
            k--;
        }
        return arr.get(arr.size()-1);
        
    }
    public void dfs(TreeNode root,ArrayList<Integer> list){
        if(root==null){
            return;
        }
        dfs(root.left,list);
        if(root!=null){
         list.add(root.val);
        }
        dfs(root.right,list);
    }
}